import { FabricTscShutUp } from "./FabricAPI/FabricTscShutUp"
import { ships } from "./Config/static";
import { Ship, Item, ItemStorage } from "./Config/types"

console.log("(Server) Hello World!");
function drive1(entity: any) {
    let angle1 = entity.direction;
    if (angle1 < 0) {
        angle1 = PI2 + angle1;
    }
    if (entity.angle < 0) {
        entity.angle = PI2 + entity.angle;
    }
    entity.angle %= PI2;
    const angle2 = entity.angle;
    if (Math.min(Math.abs(angle1 - angle2), Math.abs(PI2 - angle1 + angle2)) < 0.1) {
        entity.angle = entity.direction;
        return undefined;
    } else {
        if (angle1 > angle2) {
            if (angle1 - angle2 < Math.PI) {
                entity.angle += 0.1;
                return 0;
            } else {
                entity.angle -= 0.1;
                return 1;
            }
        } else {
            if (angle1 + (PI2 - angle2) < Math.PI) {
                entity.angle += 0.1;
                return 0;
            } else {
                entity.angle -= 0.1;
                return 1;
            }
        }
    }
}
const PI2 = Math.PI * 2

console.clear()
const spaceships = []
/*
ship
-- id
-- fuel
-- storage
-- 
*/
function initShip(entity: any, ship: Ship){
    const spaceship = FabricTscShutUp!(world.createEntity({
        mesh: ships[ship.id].mesh,
        collides: true,//碰撞
        gravity: false,//重力
        position: new GameVector3(64, 64, 64),
        meshScale: ships[ship.id].meshScale,
    }));
    spaceship.rty = ships[ship.id].initRotateY;
    spaceship.angle = 0;//玩家视角角度
    spaceship.direction = 0; 
    spaceship.rotateX = 0;
    FabricTscShutUp!(entity).spaceship = spaceship;
    spaceships.push(spaceship)
    FabricTscShutUp!(entity).up = 0;
    spaceship.speed = ships[ship.id].speed;//自己调整速度
    spaceship.rotateZ = 0;
    spaceship.angle = Math.atan2(entity.player.facingDirection.z,entity.player.facingDirection.x);
    entity.player.cameraEntity = spaceship
    entity.player.invisible = true;
    entity.player.spectator = true;
}

world.onPlayerJoin(({ entity }) => {
    initShip(entity, {
        id: "0",
        fuel: 65535,
        damage: 65535,
        item_storage: [{
            id: "1",
            count:1,
            data: {
                items: []
            }
        }]
    });
});
world.onTick(({ tick }) => {
    world.querySelectorAll('player').forEach((x) => {
        drive(x, FabricTscShutUp!(x).spaceship)
    })
})
const spaceship_Quat = new GameQuaternion(0, 0, 0, 1);
async function drive(entity: any, spaceship: any) {//太难解释了
    if (entity.player.jumpButton == true) {
        if (entity.up > -0.8) {
            entity.up -= 0.1
        }
    }
    if (entity.player.crouchButton == true) {
        if (entity.up < 0.8) {
            entity.up += 0.1
        }
    }
    spaceship.direction = Math.atan2(entity.player.facingDirection.z, entity.player.facingDirection.x)
    const num = drive1(spaceship);
    if (num === 1) {//右
        if (spaceship.rotateZ < 1) {//在这段代码中，-1和1为飞机转弯系数，系数越大能力越好
            spaceship.rotateZ += 0.1
        } else {
            spaceship.rotateZ = 1;
        }
    } else if (num === 0) {//左
        if (spaceship.rotateZ > -1) {
            spaceship.rotateZ -= 0.1
        } else {
            spaceship.rotateZ = -1;
        }
    } else if (num == undefined) {
        spaceship.rotateZ *= 0.95;
    }
    spaceship.velocity.set(Math.cos(spaceship.angle) * Math.cos(entity.up) * spaceship.speed, -Math.sin(entity.up) * spaceship.speed, Math.sin(spaceship.angle) * Math.cos(entity.up) * spaceship.speed);
    spaceship.meshOrientation = spaceship_Quat.rotateY(spaceship.rty).rotateZ(spaceship.rotateZ).rotateX(-entity.up).rotateY(spaceship.angle);
}







type remoteChannelData = {
    type: string,
    data: any
}

const remoteChannelHandlers: {
    [key: string]: (entity: GamePlayerEntity, data: remoteChannelData) => void
} = {
    "req_update_position": (entity: GamePlayerEntity, data: remoteChannelData) => {
        remoteChannel.sendClientEvent(entity, {
            "type": "update_position",
            data: FabricTscShutUp!(entity).spaceship.position
        })
    }
}

remoteChannel.onServerEvent((event) => {
    remoteChannelHandlers[FabricTscShutUp!(event.args).type](event.entity, FabricTscShutUp!(event.args));
})
